package mis.ds.ps.util;

import java.sql.DriverManager;
import java.sql.SQLException;
//import java.sql.Connection;

import mis.ds.ps.controller.Session;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.mysql.jdbc.Connection;

public class MysqlConnection {

	private static Logger logger = Logger.getLogger(Session.class);
	private static Connection mysqlConn;
	

	public static Connection getMysqlConn(Config config) {

		PropertyConfigurator.configure("log4j.properties");

		String url = "jdbc:mysql://" + config.getConfig().getString("timetable.host") + ":"
				+ config.getConfig().getString("timetable.port") + "/";
		String dbName = config.getConfig().getString("timetable.schema");
		String driver = "com.mysql.jdbc.Driver";
		String user = config.getConfig().getString("timetable.user");
		String password = config.getConfig().getString("timetable.pass");

		try {
			Class.forName(driver).newInstance();
			mysqlConn = (Connection) DriverManager.getConnection(url + dbName, user, password);
//			Class.forName("com.mysql.jdbc.Driver");
//			mysqlConn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/timetable","root","12345");


		} catch (Exception e) {
			logger.debug("Error on mySQL connection creation");
			if (mysqlConn != null) {
				try {
					mysqlConn.close();
				} catch (SQLException e1) {

					logger.debug("Error on mySQL connection close");
				}
			}
		}
		return mysqlConn;
	}
}
